<html>
<head><meta charset="utf-8"><title>code compiles · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/code.20compiles.html">code compiles</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="231018524"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/code%20compiles/near/231018524" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> kev <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/code.20compiles.html#231018524">(Mar 19 2021 at 13:01)</a>:</h4>
<p>Does anyone know why or if this code should compile?</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">enum</span> <span class="nc">Foo</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">  </span><span class="n">A</span><span class="p">(</span><span class="kt">u32</span><span class="p">),</span><span class="w"></span>
<span class="w">  </span><span class="n">B</span><span class="p">(</span><span class="kt">u32</span><span class="p">)</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">vec_of_foo</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Vec</span>::<span class="n">new</span><span class="p">();</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">mutable_foo_ref</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">None</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="mi">0</span><span class="o">..</span><span class="mi">10</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">      </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">custom_foo</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Foo</span>::<span class="n">A</span><span class="p">(</span><span class="mi">12</span><span class="p">);</span><span class="w"></span>
<span class="w">      </span><span class="n">mutable_foo_ref</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="n">custom_foo</span><span class="p">);</span><span class="w"></span>
<span class="w">      </span><span class="n">vec_of_foo</span><span class="p">.</span><span class="n">push</span><span class="p">(</span><span class="n">custom_foo</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="231018655"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/code%20compiles/near/231018655" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> kev <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/code.20compiles.html#231018655">(Mar 19 2021 at 13:02)</a>:</h4>
<p>If mutable_foo_ref is used, then compilation errors occur, was curious as to why this compiles in the first place</p>



<a name="231018672"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/code%20compiles/near/231018672" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/code.20compiles.html#231018672">(Mar 19 2021 at 13:02)</a>:</h4>
<div class="codehilite"><pre><span></span><code>warning: value assigned to `mutable_foo_ref` is never read
  --&gt; src/main.rs:11:7
   |
11 |       mutable_foo_ref = Some(&amp;mut custom_foo);
   |       ^^^^^^^^^^^^^^^
   |
   = note: `#[warn(unused_assignments)]` on by default
   = help: maybe it is overwritten before being read?
</code></pre></div>
<p>I think because the compiler detects the reference is unused, it doesn't count it as an existing borrow</p>



<a name="231019117"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/code%20compiles/near/231019117" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> kev <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/code.20compiles.html#231019117">(Mar 19 2021 at 13:06)</a>:</h4>
<p>okay got it, so there is no dead code elimination</p>



<a name="231108440"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/code%20compiles/near/231108440" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/code.20compiles.html#231108440">(Mar 20 2021 at 00:31)</a>:</h4>
<p><span class="user-mention" data-user-id="369769">@kev</span> I think <code>mutable_foo_ref</code> has type <code>Option&lt;&amp;'a mut Foo&gt;</code> where <code>'a</code> only spans the <code>mutable_foo_ref = ..</code> line. This means that it is invalid for use after the for loop (or even after the <code>push</code> call inside the for loop), but this isn't a problem because it has no drop impl</p>



<a name="231108928"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/code%20compiles/near/231108928" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Inflation <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/code.20compiles.html#231108928">(Mar 20 2021 at 00:40)</a>:</h4>
<p>If you check the MIR:<br>
<a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=b960104cf10695c3d4d7cbd51ede4dc5">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=b960104cf10695c3d4d7cbd51ede4dc5</a><br>
the lifetime of <code>&amp;mut custom_foo</code> (_16) ends right after <code>)</code> of the <code>Some</code></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>